<template>
  <div class="bg">
    <div class="login_box">
      <div class="left">
        <div class="img_box">
          <img src="../assets/img/undraw_profile_data_re_v81r 1.png" alt="">
        </div>
      </div>
      <div class="right">
        <p>用户资料完善</p>
        <el-upload
          class="avatar-uploader"
          action="https://jsonplaceholder.typicode.com/posts/"
          :show-file-list="false"
          :on-success="handleAvatarSuccess"
          :before-upload="beforeAvatarUpload">
          <img v-if="perfectForm.imageUrl" :src="perfectForm.imageUrl" class="avatar">
          <i v-else class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>
        <el-form :model="perfectForm"
         :rules="perfectRules"
          ref="perfectFormRef"
          label-position="top"
          class="perfect_form"
          >
          <el-form-item label="姓名" prop="name" class="item">
            <el-input type="text" v-model.trim="perfectForm.name"></el-input>
          </el-form-item>
          <el-form-item label="邮箱" prop="email" class="item">
            <el-input v-model.trim="perfectForm.email"></el-input>
          </el-form-item>
          <el-form-item label="生日" prop="birthday" class="item">
            <el-date-picker
             type="date"
             placeholder="选择日期"
             v-model="perfectForm.birthday"
             style="width: 100%;"
             clearable>
            </el-date-picker>
          </el-form-item>
          <el-form-item label="手机号码" prop="mobile" class="item">
            <el-input type="text" v-model.trim="perfectForm.mobile">
              <i slot="prefix" class="el-icon-phone"></i>
            </el-input>
          </el-form-item>
          <el-form-item label="性别" prop="sex" class="item">
             <el-select v-model="perfectForm.sex" placeholder="请选择" clearable>
                <el-option
                  v-for="item in options"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value">
                </el-option>
              </el-select>
          </el-form-item>
          <el-form-item class="btns">
            <el-button type="primary" @click="send">提交</el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  data () {
    // 邮箱验证规则
    var checkEmail = (rules, value, cb) => {
      const regEmail = /^[a-zA-Z0-9]+([-_.][A-Za-zd]+)*@([a-zA-Z0-9]+[-.])+[A-Za-zd]{2,5}$/
      if (regEmail.test(value)) {
        return cb()
      } else {
        return cb(new Error('请输入合法的邮箱'))
      }
    }
    return {
      perfectForm: {
        email: '',
        birthday: '',
        mobile: '',
        name: '',
        sex: '',
        imageUrl: ''
      },
      options: [{
        value: '选项1',
        label: '男'
      },
      {
        value: '选项2',
        label: '女'
      }],
      perfectRules: {
        name: [
          { required: true, message: '请输入姓名', trigger: 'blur' },
          { min: 2, max: 15, message: '长度在 2 到 15 个字符', trigger: 'blur' }
        ],
        email: [
          { required: true, message: '请输入邮箱', trigger: 'blur' },
          { validator: checkEmail, trigger: 'blur' }
        ],
        birthday: [
          { type: 'date', required: true, message: '请选择日期', trigger: 'change' }
        ],
        mobile: [
          { required: true, message: '请输入手机号', trigger: 'blur' },
          { min: 6, max: 15, message: '长度在 6 到 15 个字符', trigger: 'blur' }
        ],
        sex: [
          { required: true, message: '请选择性别', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    handleAvatarSuccess (res, file) {
      this.imageUrl = URL.createObjectURL(file.raw)
    },
    beforeAvatarUpload (file) {
      const isJPG = file.type === 'image/jpeg'
      const isLt2M = file.size / 1024 / 1024 < 2

      if (!isJPG) {
        this.$message.error('上传头像图片只能是 JPG 格式!')
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!')
      }
      return isJPG && isLt2M
    },
    send () {
      this.$refs.perfectFormRef.validate(async valid => {
        if (!valid) return
        this.$message.success('完善成功！3秒后跳转至登录页')
        this.$refs.perfectFormRef.resetFields()
        setTimeout(() => {
          this.$router.push({ path: '/login' })
        }, 3000)
      })
    }
  }
}
</script>

<style lang="less" scoped>
  .bg {
    height: 100%;
    width: 100%;
    display: flex;
    justify-content: center;
    align-items: center;
    background-color: #f7f7f7;
    overflow-y: hidden;
  }

  .login_box {
    width: 1440px;
    height: 1024px;
    background-color: #fff;
    display: flex;
  }
  .left {
    width: 720px;
    height: 1024px;
    background-color: #9A9AEB;
    position: relative;
    .img_box {
      width: 685px;
      height: 451px;
      position: absolute;
      top:226px;
      left: 18px;
      img {
        width: 100%;
        height: 100%;
      }
    }
  }
  .right {
    width: 720px;
    height: 1024px;
    position: relative;
    p {
      position: absolute;
      width: 193px;
      height: 38px;
      top: 50px;
      right: 272px;
      margin-bottom: 0px;
      color: rgb(0, 0, 0);
      font-family: Roboto;
      font-size: 32px;
      font-weight: 500;
      line-height: 38px;
      letter-spacing: 0%;
      text-align: left;
    }
  }
    .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    overflow: hidden;
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }
  .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }
  .avatar-uploader-icon{
      position: absolute;
      margin-top: 36px;
      width: 152px;
      height: 160px;
      top: 124px;
      left: 274px;
      border: 1px dashed #d9d9d9;
      border-radius: 6px;
  }
  .perfect_form {
    position: absolute;
    margin-top: 17px;
    top: 301px;
    left: 127px;
    width: 446px;
    .item ::v-deep .el-form-item__label {
      padding-top: 17px;
      color: rgb(0, 0, 0);
      font-family: Roboto;
      font-size: 24px;
      font-weight: 400;
      line-height: 28px;
      letter-spacing: 0%;
      text-align: left;
    }
    .el-input ::v-deep .el-input__inner {
      border:1px solid #4D4DDB;
    }
  }
  .btns {
    margin-top: 36px;
    display: flex;
    justify-content: center;
    .el-button {
      margin: 0px;
      width: 177px;
      height: 55px;
      background: rgb(30, 30, 30);
      border-radius: 8px;
      color: rgb(255, 255, 255);
      font-family: Roboto;
      font-size: 24px;
      font-weight: 500;
      line-height: 28px;
      letter-spacing: 0%;
      text-align: center;
    }
    .btns ::v-deep .el-button:focus, .el-button:active {
      background: #66b1ff;
      border-color: #66b1ff;
      color: #FFF;
    }
  }
  .el-date-editor.el-input{
    width: 446px;
  }
::v-deep .el-select {
                  width: 446px;
                  .el-input {
                    input {
                      height: 40px;
                      border:1px solid #4D4DDB;
                      color: #000;
                    }
                  }
                }
</style>
